const db = wx.cloud.database();
const _ = db.command;
const cloudHandler = {
    async checkLogon() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'checkLogon'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async logon(userInfo) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'logon',
                data: userInfo
            })
        } catch (e) {
            console.log(e);
        }
        return res.result;
    },
    async getGoal() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getGoal'
            })
        } catch (e) {
            console.log(e);
        }
        return res && res.result;
    },
    async postGoal(data) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'postGoal',
                data
            })
        } catch (e) {
            console.log(e);
        }
        return res && res.result;
    },
    // 获取额外资料
    async getExtraUserInfo() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getExtraUserInfo'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async postExtraUserInfo(data) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'postExtraUserInfo',
                data
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result
    },
    getPocket: (type) => {
        return cloudHandler.getCardLists(type, true);
    },
    async getCardLists(type, isActive) {
        let res;
        const query = {};
        if (isActive) {
            query.active = _.eq(true)
        }
        if (type && (type !== 'all')) {
            query.type = _.eq(type)
        };
        console.log(query);
        try {
            res = await wx.cloud.callFunction({
                name: 'getCardLists',
                query
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    // 获取某一天的打卡列表
    async getDakaRecordLists(dayString) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getDakaRecordLists',
                data: {
                    createdDay: dayString
                }
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    // 获取所有有打卡记录的日期
    async getDakaRecordListsDate() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getDakaRecordListsDate'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async delDaka(id) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'delDaka',
                data: { _id: id }
            })
        } catch (err) {
            console.log(err);
        }
        return res.result;
    },

    // 打卡操作
    async daka(data) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'postDakaRecord',
                data
            })
        } catch (err) {
            console.log(err);
        }
        return res.result;
    },
    async postCard(data) {
        // 是新建还是修改卡片信息，取决于data中是否有_id
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'postCard',
                data
            })
        } catch (err) {
            console.log(err);
        }
        return res.result;
    },
    async delCard(id) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'delCard',
                data: { _id: id }
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async getStatistics() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getStatistics'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async getDakaStatistics() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getDakaStatistics'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async getScoresStatistics(type) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getScoresStatistics',
                data: {
                    type
                }
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async initCardList() {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'initCardList'
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async getCardDetail(id) {
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'getCardDetail',
                data: { id }
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async addCardInYourPocket(id) {
        // 此id为详情id
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'addCardInYourPocket',
                data: { id }
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    },
    async shareImg(data) {
        // 此id为详情id
        let res;
        try {
            res = await wx.cloud.callFunction({
                name: 'shareImg',
                data: data
            })
        } catch (err) {
            console.log(err);
        }
        return res && res.result;
    }

};
export default cloudHandler;